<?php
namespace app\super\controller;
use think\Db;
class  AdvertController extends CommonController{

    public function getAdvert(){
        $where = '';
        $rows = $_POST['rows'];
        $page = $_POST['page'];
        if($_POST['keyword']!=''){
            $keyword = $_POST['keyword'];
            $where['ca.adv_title|ua.user_username|ua.user_phone|uu.user_username|uu.user_phone'] = array('like',"%$keyword%");
        }
        if($_POST['adv_style']!=''){//投放方式
            $where['ca.adv_style'] = $_POST['adv_style'];
        }
        if($_POST['adv_status']!=''){//状态
            $where['ca.adv_status'] = $_POST['adv_status'];
        }
        if($_POST['start_release']!=''){//发布时间
            $where['ca.adv_time'] =$_POST['start_release'];
        }
//        if($_POST['start_put']!=''){//投放开始时间
//            $where['ca.adv_start'] =$_POST['start_put'];
//        }
//        if($_POST['end_put']!=''){//投放结束时间
//            $where['ca.adv_end'] =$_POST['end_put'];
//        }

        $res = Db::table('up_commerce_adv')
            ->alias('ca')
            ->join('up_u_user uu','ca.adv_user_id = uu.user_id','left')
            ->join('up_u_admin ua','ca.adv_user_id = ua.user_id','left')
            ->where($where)
            ->limit($rows * ($page - 1), $rows)
            ->field('uu.user_username,ua.user_username as user_name,ca.adv_title,ca.adv_style,ca.adv_range,ca.adv_start,ca.adv_end,ca.adv_count,ca.adv_status,ca.adv_total_money,ca.adv_time,ca.adv_id')
            ->order('ca.adv_time DESC')
            ->select();

        $total = Db::table('up_commerce_adv')->alias('ca')
            ->join('up_u_user uu','uu.user_id = ca.adv_user_id','left')
            ->join('up_u_admin ua','ua.user_id = ca.adv_user_id','left')
            ->where($where)
            ->count();

        $data['total'] = $total;
        $data['rows'] = $res;
        return json_encode($data);
    }
    /**
     * 广告分类列表
     */
    public function getAdvertisement(){
        $rows = $_POST['rows'];
        $page = $_POST['page'];
        $where['region_fid'] = 'P48DLGW9He9A63HFNyBqPnXxXTvdclq2';
        $res = Db::table('up_dic_region')
            ->where($where)
            ->limit($rows * ($page - 1), $rows)
            ->select();
        $total = Db::table('up_dic_region')
            ->where($where)
            ->limit($rows * ($page - 1), $rows)
            ->count();
        $data['total'] = $total;
        $data['rows'] = $res;
        return json_encode($data);
    }

    /***
     * 删除行业分类信息
     */
    public function delIndustry(){
        $cid = $_POST['cid'];
        $res = Db::table('up_dic_region')->where('region_id',$cid)->delete();
        if($res){
            $returnData['status'] = 1;
            $returnData['msg'] = "删除成功";
            return json_encode($returnData);
        }else{
            $returnData['status'] = 0;
            $returnData['msg'] = "删除失败";
            return json_encode($returnData);
        }
    }

    /**
     * 添加分类页面
     */
    public function addIndustry(){
        return $this->fetch('advertisement/addIndustry');
    }

    public function insertIndustry(){
        $where['region_name'] = $_POST['region_name'];
        $where['region_fid'] = 'P48DLGW9He9A63HFNyBqPnXxXTvdclq2';
        $res = Db::table('up_dic_region')->where($where)->find();
        if($res['region_id']){
            $returnData['status'] = 0;
            $returnData['msg'] = "广告分类存在相同的分类，不能重复添加";
            return json_encode($returnData);
        }
        $data['region_name'] = $_POST['region_name'];
        $data['region_id'] = $this->getRandChar(32);
        $data['region_fid'] = 'P48DLGW9He9A63HFNyBqPnXxXTvdclq2';
        $result = Db::table('up_dic_region')->insert($data);
        if($result){
            $returnData['status'] = 1;
            $returnData['msg'] = "添加成功";
            return json_encode($returnData);
        }else{
            $returnData['status'] = 0;
            $returnData['msg'] = "添加失败";
            return json_encode($returnData);
        }
    }

    /**
     * 编辑页面
     */
    public function updateIndustry(){
        $res = Db::table('up_dic_region')->where('region_id',$_POST['nid'])->find();
        $this->assign('res',$res);
        return $this->fetch('advertisement/updateIndustry');
    }
    /**
     * 编辑功能
     */
    public function updateIndustryInfo(){
        $where['region_id'] = array('NEQ',$_POST['region_id']);
        $where['region_fid'] = 'P48DLGW9He9A63HFNyBqPnXxXTvdclq2';
        $where['region_name'] = $_POST['region_name'];
        $res = Db::table('up_dic_region')->where($where)->find();
        if($res['region_id']){
            $returnData['status'] = 0;
            $returnData['msg'] = "行业分类存在相同的行业，不能重复添加";
            return json_encode($returnData);
        }else{
            $result = Db::table('up_dic_region')->where('region_id',$_POST['region_id'])->setField('region_name',$_POST['region_name']);
            if($result){
                $returnData['status'] = 1;
                $returnData['msg'] = "编辑成功";
                return json_encode($returnData);

            }else{
                $returnData['status'] = 0;
                $returnData['msg'] = "编辑失败";
                return json_encode($returnData);
            }
        }
    }

    /**
     * releaseMessage 发布广告
     * @return 返回状态
     */
    public function releaseAdv(){
        $cid = $_POST['cid'];
        $returnData = $this->operation($cid,4,"发布成功","发布失败");
        return json_encode($returnData);
    }
    /**
     * cancelAdv 取消发布广告
     * @return 返回状态
     */
    public function cancelAdv(){
        $cid = $_POST['cid'];
        $returnData = $this->operation($cid,6,"取消发布成功","取消发布失败");
        return json_encode($returnData);
    }

    /**
     * operation 公共方法操作数据库
     * @param $cid 编号 $num 修改值 $success成功信息 $failed失败信息
     * @return 返回状态
     */
    public function operation($cid,$num,$success,$failed){
        if($cid!=''){
            $where['adv_id'] = $cid;
            $data['adv_status'] = $num;//修改数据库值
            $res =  Db::table('up_commerce_adv')->where($where)->update($data);
            if($res){
                $returnData['status'] = 1;
                $returnData['msg'] = $success;
            }else{
                $returnData['status'] = 0;
                $returnData['msg'] = $failed;
            }
        }else{
            $returnData['status'] = 0;
            $returnData['msg'] = "传递参数错误";
        }
        return $returnData;
    }

    /**
     * audit 展示审核页面
     * @return 返回页面
     */
    public function audit(){
        $cid = $_POST['cid'];
        $data = Db::table('up_commerce_adv')
            ->alias('uca')
            ->where(['adv_id'=>$cid])
            ->field('uca.adv_title,uca.adv_content,uca.adv_imgs,uca.adv_range,uca.adv_banner_img,uca.adv_style,uca.adv_type')
            ->find();
        if($data['adv_style']==1){
            $region_name =Db::table('up_dic_region')->where('region_id',$data['adv_type'])->value('region_name');
            $data['region_name'] = $region_name;
        }else{
            $data['region_name'] = '';
        }
        $data['adv_imgs'] = explode(',',$data['adv_imgs']);
        $this->assign('cid',$cid);
        $this->assign('data',$data);
        return $this->fetch('advert/audit');
    }
    /**
     *  审核
     * @return 返回修改数据库状态
     */
    public function pass(){
        $where['adv_id'] = input('adv_id');
        if($_POST['status']==2){
            $status = 1;
            if(trim($_POST['reason'])==''){
                $returnData['msg']= '审核不通过原因不能为空';
                $returnData['status']= 0;
                return json_encode($returnData);

            }else{
                $dataS['adv_reason'] = $_POST['reason'];
            }
        }else{
            $status =3;
        }
        $dataS['adv_status'] = $status;
        $fid = input('adv_id');
        $res = Db::table('up_commerce_adv')->where($where)->update($dataS);
        $adv_user_id = Db::table('up_commerce_adv')->where($where)->value('adv_user_id');
        $adv_style = Db::table('up_commerce_adv')->where($where)->value('adv_style');
        if($res){
            if($_POST['status']==2){
                $msg1 = "{\"msg\":\"27\"}";
                $msg = '您提交的发布广告信息申请已审核未通过,快去查看原因！';
                $msg_data= array('msg'=>$msg,'code'=>27,'fid'=>$adv_user_id,'type'=>$adv_style,'adv_id'=>$fid);
                $code = 27;
                $code1 = 27;
                $this->sendJpush(1,$msg,$msg_data,$adv_user_id,$msg1,$code,2,$code1);
            }else{
                $msg1 = "{\"msg\":\"26\"}";
                $msg = '您提交的发布广告信息申请已审核通过,快去设置广告红包！';
                $msg_data= array('msg'=>$msg,'code'=>26,'fid'=>$adv_user_id,'type'=>$adv_style,'adv_id'=>$fid);
                $code = 26;
                $code1 = 26;
                $this->sendJpush(1,$msg,$msg_data,$adv_user_id,$msg1,$code,2,$code1);
            }
            $returnData['status'] = 1;
            $returnData['msg'] = "审核成功";
            return json_encode($returnData);
        }else{
            $returnData['status'] = 0;
            $returnData['msg'] = "审核失败";
            return json_encode($returnData);
        }
    }

    /**
     * delAdvert 删除广告信息
     * @return 返回修改数据库状态
     */
    public function delAdvert(){
        $where['adv_id'] = input('cid');
        $res = Db::table('up_commerce_adv')
            ->where($where)
            ->delete();
        if($res){
            $returnData['status'] = 1;
            $returnData['msg'] = "删除成功！";
        }else{
            $returnData['status'] = 0;
            $returnData['msg'] = "删除失败！";
        }
        return json_encode($returnData);
    }

    /**
     * showAdv 展示修改广告页面
     * @return 返回页面
     */
    public function showAdv(){
        $where['adv_id'] = input('cid');
        $data = Db::table('up_commerce_adv')
            ->alias('uca')
            ->join('up_dic_region udr','uca.adv_type = udr.region_id')
            ->where($where)
//            ->field('uca.adv_style,uca.adv_range,uca.adv_gender,uca.adv_age,uca.adv_type,uca.adv_title,uca.adv_content,uca.adv_province,uca.adv_city,uca.adv_district,uca.adv_time_length,uca.adv_count_no,uca.adv_count_yes')
            ->find();
        $pro = Db::table('up_area')->where('district_id',$data['adv_province'])->value('district');
        $cit = Db::table('up_area')->where('district_id',$data['adv_city'])->value('district');
        $are = Db::table('up_area')->where('district_id',$data['adv_district'])->value('district');
        $province = Db::table('up_area')->where('level',1)->select();
        $city =Db::table('up_area')->where('level',2)->field('district_id,district')->select();
        $area =Db::table('up_area')->where('level',3)->field('district_id,district')->select();
        $info = Db::table('up_dic_region')
            ->where('region_fid','P48DLGW9He9A63HFNyBqPnXxXTvdclq2')
            ->field('region_id,region_name')
            ->select();
        //编辑对应的处理——6张详情图
        $for_loop = array(4,5,6,7,8,9);
        $sixid = array();
        $sixsrc = array();
        $sixhave = array();
        $next = 4;
        $isten = 4;
        if($data['adv_imgs']){
            $six = explode(',',$data['adv_imgs']);
            $i = 4;
            foreach ($six as $v){
                $sixsrc[$i] = $v;
                $vv = explode('/',$v);
                $count = count($vv);
                $vvv = $vv[$count-1];
                $vvvv = explode('.',$vvv);
                $sixid[$i] = $vvvv[0];
                $sixhave[] = $i;
                $i++;
                $next = $i;
                $isten = $i;
            }
        }
//        var_dump($sixid);die;
        if($next > 9){$next = 4;}
        $this->assign('for_loop',$for_loop);
        $this->assign('sixid',$sixid);
        $this->assign('sixsrc',$sixsrc);
        $this->assign('sixhave',$sixhave);
        $this->assign('next',$next);
        $this->assign('isten',$isten);

//        $this->assign('imgs',$imgs);
        $this->assign('province',$province);
        $this->assign('info',$info);
        $this->assign('city',$city);
        $this->assign('area',$area);
        $this->assign('data',$data);
        $this->assign('pro',$pro);
        $this->assign('cit',$cit);
        $this->assign('are',$are);
        return $this->fetch('advert/editadv');
    }

    /**
     * insertAdvPic 异步上传图片
     * @return string
     */
    public function insertAdvPic()
    {
        $who = $_POST['who'];
        $name = $_FILES["thumb{$who}"]['name'];
        $name = explode('.',$name);
        $date = date('YmdHis');
        $rand = rand(100000,999999);
        $aa = $date . $rand . '.' . $name[1];
        $dir = $_SERVER['DOCUMENT_ROOT'];
        $url =  "http://".$_SERVER['HTTP_HOST'];
        if($name[1] != 'jpg' && $name[1] != 'png' && $name[1] != 'gif'){
            return json_encode(['status'=>0,'msg'=>'上传的图片格式不支持']);
        }
        if(!empty($_FILES["thumb{$who}"]['tmp_name'])){
            if(move_uploaded_file($_FILES["thumb{$who}"]['tmp_name'],"{$dir}/upload/{$aa}")){
                $msg = "{$url}/upload/{$aa}";
                $this->image_png_size_add("{$dir}/upload/{$aa}","{$dir}/upload/{$aa}");
                return json_encode(['status'=>200,'msg'=>$msg,'idid'=>$date.$rand]);
            }else{
                return json_encode(['status'=>0,'msg'=>'图片保存失败']);
            }
        }else{
            return json_encode(['status'=>0,'msg'=>'获取不到图片信息']);
        }
    }

    /**
     * update 修改数据库数据
     * @return 返回操作数据库状态
     */
    public function update(){

        if(!empty($_FILES['fileZ']['name'])){
            //上传头像
            $rands = date('YmdHis').rand(100000,999999);
            $exes = explode('.',$_FILES['fileZ']['name']);
            $filePaths = $_FILES['fileZ']['tmp_name'];

            $infos = $this->qnCommon(2,$rands,$exes[1],$filePaths);
            if($infos['status']!=200) {
                $returnData['status'] = 0;
                $returnData['msg'] = "上传文件格式不正确";
                return json_encode($returnData);
            }
            $data['adv_banner_img'] =QiNiu .$infos['key'];
        }
        $where['adv_id'] = $_POST['adv_id'];
        $data['adv_style'] = $_POST['style'];
        if($_POST['style']==2){
            $data['adv_range'] = $_POST['region'];
        }
        if($_POST['province']!=="" && $_POST['city']!=="" && $_POST['area']!==""){
            $data['adv_province'] = $_POST['province'];
            $data['adv_city'] = $_POST['city'];
            $data['adv_district'] = $_POST['area'];
        }
        $data['adv_gender'] = $_POST['sex'];
        $data['adv_age'] = $_POST['age'];
        $data['adv_type'] = $_POST['region_name'];
        if($_POST['adv_title']!==""){
            $data['adv_title'] = $_POST['adv_title'];
        }else{
            $returnData['status'] = 0;
            $returnData['msg'] = "广告标题不能为空";
        }
        if($_POST['adv_content']!==""){
            $data['adv_content'] = $_POST['adv_content'];
        }else{
            $returnData['status'] = 0;
            $returnData['msg'] = "广告内容不能为空";
        }
        if(isset($_POST['insert_image'])){
            $img_array = $_POST['insert_image'];
            if($img_array){
                $data['adv_imgs']=implode(',',$img_array);
            }
        }
        $res = Db::table('up_commerce_adv')->where($where)->update($data);
        if($res){
            $returnData['status'] = 200;
            $returnData['msg'] = "修改成功";
        }else{
            $returnData['status'] = 1;
            $returnData['msg'] = "修改失败";
        }
        return json_encode($returnData);
    }
}